<?php

namespace app\backend\controller;

use app\backend\model\AuthGroup;
use app\backend\model\AuthGroupAccess;
use app\backend\model\AuthRule;
use app\common\auth\Auth;
use app\common\model\AdminUsers;
use Redis;
use think\Controller;
use think\Request;

//权限管理
class Power extends Controller
{

    /**权限管理**/
    //权限列表
    public function permissions()
    {
        //auth_rule表信息
        $rule_info=AuthRule::useGlobalScope(false)->select();

        $this->assign(compact('rule_info'));
        return $this->fetch();
    }
    //增加权限
    public function addPermissions(Request $request)
    {
        if($request->isGet()) {
            return $this->fetch();
        }

        //1. 获取请求数据
        $res=$request->post();
        $name=$request->post('name');
        $title=$request->post('title');

        //2. 验证数据
        $err_msg=$this->validate($request->post('','','trim,strip_tags'),[
            'name'=>'require|max:30|unique:auth_rule|token',
            'title'=>'require|max:100'
        ],[
            'name.require'=>'请填写权限名',
            'name.max'=>'权限名不能超过30字符',
            'name.unique'=>'权限名已存在',
            'title.require'=>'请填写描述',
            'title.max'=>'描述不能超过100字符'
        ]);

        if($err_msg !==true){
            return renderJs($err_msg);
        }

        //写入数据库
        $auth_rule=new AuthRule;
        $auth_rule->name=$name;
        $auth_rule->title=$title;
        $auth_rule->save();

        $this->redirect('/admin/permissions');
    }
    //修改权限
    public function update($id,Request $request)
    {
        if($request->isGet()) {
            //获取权限信息 auth_rule
            $rule_info = AuthRule::get($id);

            if (!$rule_info) {
                return renderJs('不存在的权限','/admin/permissions');
            }

            $this->assign(compact('rule_info'));
            return $this->fetch('update_permissions');
        }

        #var_dump($request->post());

        //获取请求
        $name=$request->post('name','','trim,strip_tags');
        $title=$request->post('title','','trim,strip_tags');



        //验证
        ///检验是否修改

        $err_msg=$this->validate($request->post('','','trim,strip_tags'),[
            'name'=>"require|max:30|unique:auth_rule,name,$id|token", //判断,看看,如果没修改则不判断,修改则判断唯一性
            'title'=>'require|max:100'
        ],[
            'name.require'=>'请填写权限名',
            'name.max'=>'权限名不能超过30字符',
            'name.unique'=>'权限名已存在',
            'title.require'=>'请填写描述',
            'title.max'=>'描述不能超过100字符'
        ]);

        if($err_msg !==true){
            return renderJs($err_msg);
        }

        $auth_info=AuthRule::get($id);

        if( ($auth_info['name'] == $name) && ($auth_info['title'] == $title))
        {
            return renderJs('无修改');
        }else if($auth_info['name'] !== $name){
            $auth_info->name=$name;
        }else if($auth_info['title'] !== $title){
            $auth_info->title=$title;
        }else{
            return renderJs('非法操作');
        }

        $auth_info->save();

        $this->redirect('/admin/permissions');
    }
    //删除权限
    public function delete(Request $request)
    {
        //获取请求信息
        $id=$request->post('id',0,'trim,strip_tags,intval');

        //获取对应权限信息,并修改状态为0
        $rule_info=AuthRule::get($id);

        if(!$rule_info){
            return json(['msg'=>'非法操作','code'=>-1]);
        }

        $rule_info->status=0;
        $rule_info->save();

        return json(['msg'=>'删除权限成功' ,'code'=>200]);

    }
    //恢复权限
    public function recover(Request $request)
    {
        //获取请求信息
        $id=$request->post('id',0,'trim,strip_tags,intval');

        //获取对应权限信息,并修改状态为1
        $rule_info=AuthRule::useGlobalScope(false)->where('status',0)->where('id',$id)->find();

        if(!$rule_info){
            return json(['msg'=>'非法操作','code'=>-1]);
        }

        $rule_info->status=1;
        $rule_info->save();

        return json(['msg'=>'恢复权限成功','code'=>200]);
    }



    /**用户管理**/
    //用户列表
    public function users(Request $request)
    {
        if($request->isGet()) {
            //admin_users表
            $admin_info=AdminUsers::where('status',1)->select();

            $this->assign(compact('admin_info'));
            return $this->fetch();
        }
    }
    //添加用户
    public function addUsers(Request $request)
    {
        if($request->isGet()) {
            return $this->fetch();
        }

        //获取请求信息
        $nickname=$request->post('name');
        $pwd=$request->post('password');

        //验证
        $err_msg=$this->validate($request->post('','','trim,strip_tags'),[
            'name'=>'require|max:20|unique:admin_users,nickname|token',
            'password'=>'require|max:20'
        ],[
            'name.require'=>'请填写用户名',
            'name.max'=>'用户名不能大于20字符',
            'name.unique'=>'用户名已存在',
            'password.require'=>'请填写密码',
            'password.max'=>'密码不能大于20字符'
        ]);

        if($err_msg !==true){
            return renderJs($err_msg);
        }

        #var_dump($request->post('','','trim,strip_tags'));

        //写入数据库
        $admin_user=new AdminUsers;
        $admin_user->nickname=$nickname;
        $admin_user->pwd=password_hash($pwd,PASSWORD_BCRYPT);
        $admin_user->save();

        $this->redirect('/admin/users');
    }
    //用户设置角色
    public function usersRole($id,Request $request)
    {

        if($request->isGet()) {
            //显示角色列表
            $group_list=AuthGroup::where('status',1)->select();

            //是否存在id=>uid 的 auth_group_access
            $group_id=AuthGroupAccess::where('uid',$id)->value('group_id');

            $this->assign(compact('group_list','group_id'));
            return $this->fetch();
        }

        //获取请求
        $group_id=intval($request->post('group_id'));

        //验证
        $err_msg=$this->validate($request->post(),[
            'group_id'=>'require|token'
        ],[
            'group_id.require'=>'请给用户设置角色'
        ]);

        if($err_msg !==true){
            return renderJs($err_msg);
        }

        //写入数据库
        ///检测是否存在id
        $group_access_info=AuthGroupAccess::where('uid',$id)->find();

        if(!$group_access_info){
            $group_access_info=new AuthGroupAccess;
        }

        $group_access_info->uid=$id;
        $group_access_info->group_id=$group_id;
        $group_access_info->save();

        return $this->redirect('/admin/users');
    }

    /**角色管理 **/
    //角色列表
    public function roles()
    {
        //获取角色信息
        $auth_group=AuthGroup::where('status',1)->select();

        $this->assign(compact('auth_group'));
        return $this->fetch();
    }
    //添加角色
    public function addRoles(Request $request)
    {
        if($request->isGet()) {
            return $this->fetch();
        }

        //获取请求
        $title=$request->post('title','','trim,strip_tags');
        $description=$request->post('description');

        //验证
        $err_msg=$this->validate($request->post('','','trim,strip_tags'),[
            'title'=>'require|max:100|unique:auth_group|token',
            'description'=>'require|max:100'
        ],[
            'title.require'=>'角色名不能为空',
            'title.max'=>'角色名最大值不超过100字符',
            'title.unique'=>'此角色名已存在',
            'description.require'=>'请填写描述信息',
            'description.max'=>'描述信息最大值不超过100字符'
        ]);

        if($err_msg !== true){
            return renderJs($err_msg);
        }

        //写入数据库
        $auth_group=new AuthGroup;
        $auth_group->title=$title;
        $auth_group->description=$description;
        $auth_group->save();

        $this->redirect('/admin/roles');
    }
    //角色设置权限
    public function rolesPer($id,Request $request)
    {
        if($request->isGet()) {
            //获取权限列表
            $rule_list = AuthRule::where('status', 1)->select();

            //查看是否角色 有 权限
            $this_rules=AuthGroup::where('status',1)->where('id',$id)->value('rules');

            //格式化将字符串转成数组
            $array_rules=explode(',',$this_rules);
            $this->assign(compact('rule_list','array_rules'));
            return $this->fetch();
        }


        //获取请求
        $perms=$request->post('permissions/a');


        //验证
        $err_msg=$this->validate($request->post(),[
            'permissions'=>'require|token'
        ],[
            'permissions.require'=>'请给角色权限'
        ]);

        if($err_msg !==true){
            return renderJs($err_msg);
        }

        //格式化数据 rules
        $rules=implode(',',$perms);

        //写入auth_group
        $auth_group_info=AuthGroup::where('status',1)->where('id',$id)->find();

        if(!$auth_group_info){
            return renderJs('不存在的角色信息');
        }

        $auth_group_info->rules=$rules;
        $auth_group_info->save();

        $this->redirect('/admin/roles');
    }
}
